package com.wanxi.service.impl;

import com.wanxi.dao.UserDao;
import com.wanxi.dao.impl.UserDaoImpl;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.UserModel;
import com.wanxi.service.UserService;
import com.wanxi.util.JSONCopy;
import com.wanxi.util.Md5;
import com.wanxi.vo.UserVO;

import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    /**
     * @param userVO
     * @return
     */
    @Override
    public ResultDTO login(UserVO userVO) {
        ResultDTO resultDTO = new ResultDTO();
//        判断验证码是否正确
        if (userVO.getCode().equals(userVO.getSessionCode())) {

//            判断用户名和密码是否正确
//            vo不能进入dao层：只能止步于servlet和service
//            dao层只能用model
//            所以需要将vo里面的数据赋值到model
            UserModel userModel = new UserModel();
//            userModel.setUsername(userVO.getUsername());
//            userModel.setPassword(userVO.getPassword());
//            所以需要一个工具来做？
            userModel = JSONCopy.copy(userVO, UserModel.class);
            String mingwen = userModel.getPassword();
            String anwen = Md5.encoderByMd5(mingwen);
            userModel.setPassword(anwen);

            UserModel model = userDao.findByUsernameAndPassword(userModel);
            if (model != null) {
                resultDTO.setCode(1);
                resultDTO.setMsg("success");
            } else {
                resultDTO.setCode(0);
                resultDTO.setMsg("error");
            }

        } else {
            resultDTO.setCode(-1);
            resultDTO.setMsg("error-code");
        }
        return resultDTO;
    }

    @Override
    public ResultDTO add(UserVO userVO) {
        UserModel model = JSONCopy.copy(userVO, UserModel.class);
//        String mingwen = model.getPassword();
//        String anwen = Md5.encoderByMd5(mingwen);
//        model.setPassword(anwen);
        model.setPassword(Md5.encoderByMd5(model.getPassword()));
//        对于dao层来说，返回的应该是影响的条数
        int count = userDao.add(model);
        ResultDTO resultDTO = ResultDTO.builder().count(count).build();
        return resultDTO;
    }

    @Override
    public ResultDTO update(UserVO userVO) {

        UserModel model = JSONCopy.copy(userVO, UserModel.class);
        int count = userDao.update(model);
        ResultDTO resultDTO = ResultDTO.builder().count(count).build();
        return resultDTO;
    }

    /**
     * 修改密码逻辑
     *
     * @param userVO
     * @return
     */
    @Override
    public ResultDTO updatePassword(UserVO userVO) {
        /**
         * 1.根据id或者用户名查询原密码是否正确
         * 2.如果不正确，则不能修改
         * 3.如果正确，则直接修改
         */
        ResultDTO resultDTO = ResultDTO.builder().build();
        UserModel model = JSONCopy.copy(userVO, UserModel.class);
        model.setPassword(Md5.encoderByMd5(userVO.getOldPassword()));
//        UserModel userModel = userDao.findByUsernameAndPassword(model);
        UserModel userModel = userDao.findByIdAndPassword(model);
        if (userModel == null) {
            resultDTO.setCode(-1);
            resultDTO.setMsg("原密码不正确，请重新输入");
        } else {
            model.setPassword(Md5.encoderByMd5(userVO.getNewPassword()));
            int count = userDao.updatePassword(model);
            if (count > 0) {
                resultDTO.setCode(0);
                resultDTO.setMsg("修改成功");
            } else {
                resultDTO.setCode(-1);
                resultDTO.setMsg("修改失败");
            }
        }

        return resultDTO;
    }

    @Override
    public ResultDTO delete(UserVO userVO) {
        ResultDTO resultDTO = ResultDTO.builder().build();
        UserModel model = JSONCopy.copy(userVO, UserModel.class);
        int count = userDao.delete(model);
        if (count > 0) {
            resultDTO.setCode(0);
            resultDTO.setMsg("删除成功");
        } else {
            resultDTO.setCode(-1);
            resultDTO.setMsg("删除失败");
        }
        return resultDTO;
    }

    @Override
    public ResultDTO findById(UserVO userVO) {
        ResultDTO resultDTO = ResultDTO.builder().build();
        UserModel model = JSONCopy.copy(userVO, UserModel.class);
        UserModel userModel = userDao.findById(model);
        if (userModel == null) {
            resultDTO.setCode(-1);
            resultDTO.setMsg("没有查询到数据");
        } else {
            resultDTO.setCode(1);
            resultDTO.setMsg("");
            resultDTO.setData(userModel);
        }
        return resultDTO;
    }

    @Override
    public ResultDTO findAll(UserVO userVO) {
//        ResultDTO resultDTO = ResultDTO.builder().build();
        UserModel model = JSONCopy.copy(userVO, UserModel.class);
//        List<UserModel> userModelList = userDao.findAll(model);
//        resultDTO.setCode(1);
//        resultDTO.setMsg("");
//        resultDTO.setCount(userModelList.size());
//        resultDTO.setData(userModelList);
//        return resultDTO;
        return ResultDTO.getDTO(userDao.count(model), userDao.findAll(model));
    }
}
